package leetcode.day_2020_2021_2_1_2021_2_7;

import java.util.Stack;

/**
 * 20. 有效的括号
 * https://leetcode-cn.com/problems/valid-parentheses/
 */
public class LeetCode0020 {
    public static void main(String[] args) {
        System.out.println(isValid("()"));
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if(c=='(' || c=='[' || c=='{') {
                stack.push(c);
            } else if(c == stack.peek()) {
                stack.pop();
            } else {
                return false;
            }
        }
        if(stack.isEmpty()) return true;
        return false;
    }
}
